package cn.hsa.fqz.task.service.impl;

import cn.hsa.fqz.common.enums.FileType;
import cn.hsa.fqz.task.dao.TTaskClueSubjectMapper;
import cn.hsa.fqz.task.dao.TTaskWorkReportMapper;
import cn.hsa.fqz.task.entity.TSysFileDO;
import cn.hsa.fqz.task.entity.TTaskClueSubject;
import cn.hsa.fqz.task.entity.TTaskWorkReport;
import cn.hsa.fqz.task.enums.UserCategoryEnums;
import cn.hsa.fqz.task.service.TClueTaskService;
import cn.hsa.fqz.task.service.TSysFileAccessoryService;
import cn.hsa.fqz.task.service.dto.LoginUserDTO;
import cn.hsa.fqz.task.service.dto.TaskClueQueryDTO;
import cn.hsa.fqz.util.UserRegionUtils;
import cn.hsa.hsaf.core.framework.util.PageResult;
import cn.hsa.hsaf.core.framework.web.WrapperResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author 丁志敏
 * @version v1.0
 * @apiNote 功能描述:()
 * @date create in 2022/9/21 17:17
 */
@Slf4j
@Service
public class TClueTaskServiceImpl implements TClueTaskService {

    @Autowired
    private TTaskClueSubjectMapper tTaskClueSubjectMapper;
    @Autowired
    private TSysFileAccessoryService sysFileAccessoryService;
    @Autowired
    private TTaskWorkReportMapper tTaskWorkReportMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public WrapperResponse<Boolean> addPutin(LoginUserDTO loginUser, TTaskClueSubject subject) {
        subject.setAddUser(loginUser.getUid());
        subject.setComUsers(loginUser.getUname());
        UserCategoryEnums enums = UserRegionUtils.getUserCateEnumByAscm(loginUser.getRegion());
        subject.setBelongTo(enums.name());
        subject.setCsDate(new Date());
        int count = tTaskClueSubjectMapper.insertSelective(subject);
        if(count > 0 && !CollectionUtils.isEmpty(subject.getFileNos())){
            for (String fileNo:subject.getFileNos()) {
                sysFileAccessoryService.add(subject.getCsId()+"", FileType.CE.getValue(),fileNo);
            }
            return WrapperResponse.success(Boolean.TRUE);
        }
        return WrapperResponse.success(Boolean.FALSE);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public WrapperResponse<Boolean> update(LoginUserDTO loginUser, TTaskClueSubject subject) {
        UserCategoryEnums enums = UserRegionUtils.getUserCateEnumByAscm(loginUser.getRegion());
        subject.setBelongTo(enums.name());
        int count = tTaskClueSubjectMapper.updateByPrimaryKeySelective(subject);
        if(count > 0 && !CollectionUtils.isEmpty(subject.getFileNos())) {
            return WrapperResponse.success(Boolean.TRUE);
        }
        return WrapperResponse.success(Boolean.FALSE);
    }

    @Override
    public WrapperResponse<PageResult<TTaskClueSubject>> getPageByCondition(TaskClueQueryDTO query) {
        Page<TTaskClueSubject> page =  PageHelper.startPage(query.getPage().getPageNum(), query.getPage().getPageSize());
        List<TTaskClueSubject> list = tTaskClueSubjectMapper.getPageByCondition(query.getCondition());

        PageResult<TTaskClueSubject> result = new PageResult<>();
        result.setData(list);
        result.setPageNum(page.getPageNum());
        result.setPageSize(page.getPageSize());
        result.setPages(page.getPages());
        result.setRecordCounts((int)page.getTotal());
        return WrapperResponse.success(
                result
        );

//
//        return WrapperResponse.success(
//                IPageHelper.toPage(new PageInfo<>(list), TTaskClueSubject.class)
//        );
    }

    @Override
    public WrapperResponse<PageResult<TTaskClueSubject>> getPageAuditByCondition(TaskClueQueryDTO query) {
        Page<TTaskClueSubject> page = PageHelper.startPage(query.getPage().getPageNum(), query.getPage().getPageSize());
        List<TTaskClueSubject> list = tTaskClueSubjectMapper.getPageAuditByCondition(query.getCondition());

        PageResult<TTaskClueSubject> result = new PageResult<>();
        result.setData(list);
        result.setPageNum(page.getPageNum());
        result.setPageSize(page.getPageSize());
        result.setPages(page.getPages());
        result.setRecordCounts((int)page.getTotal());
        return WrapperResponse.success(
                result
        );
//        return WrapperResponse.success(
//                IPageHelper.toPage(new PageInfo<>(list), TTaskClueSubject.class)
//        );
    }

    @Override
    public WrapperResponse<Boolean> addAudit(LoginUserDTO loginUser, Integer subjectId,Integer isAudit,String rejectReason) {
        if(isAudit == 0 && StringUtils.isEmpty(rejectReason)){
            return WrapperResponse.error(-1,"请填写审核不通过原因",null);
        }
        TTaskClueSubject subject = tTaskClueSubjectMapper.selectByPrimaryKey(subjectId);
        subject.setAuditStatus(isAudit);
        if(isAudit == 0){
            subject.setRejectReason(rejectReason);
        }
        if(subject.getCsSources().equals("8")){ //当前任务申报为工作汇报类型时
           insertTaskWorkReport(subject);
        }
        return WrapperResponse.success(tTaskClueSubjectMapper.updateByPrimaryKeySelective(subject) > 0);
    }

    private void insertTaskWorkReport(TTaskClueSubject subject) {
        TTaskWorkReport report = new TTaskWorkReport();
        report.setTitle(subject.getCsTitle());
        report.setYears(subject.getYearmutil());
        report.setContent(subject.getCsContent());
        report.setCreateBy(subject.getAddUser());
        report.setCreateByName(subject.getComUsers());
        report.setCreateTime(new Date());
        tTaskWorkReportMapper.insertSelective(report);
    }

    @Override
    public WrapperResponse<Boolean> edit(LoginUserDTO loginUser, TTaskClueSubject subject) {
        TTaskClueSubject dbSubject = tTaskClueSubjectMapper.selectByPrimaryKey(subject.getCsId());
        if(Objects.isNull(dbSubject)){
           return WrapperResponse.error(-1,"不存在记录",null);
        }
        if(dbSubject.getAuditStatus() == 1){
            return WrapperResponse.error(-1,"已审核，不可编辑",null);
        }
        UserCategoryEnums enums = UserRegionUtils.getUserCateEnumByAscm(loginUser.getRegion());
        subject.setBelongTo(enums.name());
        int count = tTaskClueSubjectMapper.updateByPrimaryKeySelective(subject);
        if(count > 0 && !CollectionUtils.isEmpty(subject.getFileNos())){
            return WrapperResponse.success(Boolean.TRUE);
        }
        return WrapperResponse.success(Boolean.FALSE);
    }

    @Override
    public WrapperResponse<TTaskClueSubject> getById(Integer subjectId) {
        TTaskClueSubject clueSubject = tTaskClueSubjectMapper.selectByPrimaryKey(subjectId);
        List<TSysFileDO> fileList = sysFileAccessoryService.list(subjectId+"",FileType.CE);
        clueSubject.setFiles(fileList);
        return WrapperResponse.success(clueSubject);
    }
}
